package bb.lanxing.nav;

import com.baidu.mapapi.model.LatLng;
import com.github.mikephil.charting.utils.Utils;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import bb.lanxing.App;
import bb.lanxing.R;
import bb.lanxing.model.json.Route;
import bb.lanxing.model.json.RouteLeg;
import bb.lanxing.model.json.RoutePoint;
import bb.lanxing.model.json.RoutePolyline;
import bb.lanxing.model.json.RouteStep;
import bb.lanxing.util.BiCiCoorConverter;
import bb.lanxing.util.Log;
import bb.lanxing.util.PolyUtil;
import bb.lanxing.util.map.SphericalUtil;

public class PolyAnalyzer {
    private static final double STEP_POLYLINE_TOLERANCE = 10.0d;
    private static final double TOLERANCE = 20.0d;

    private double toRadian(int i) {
        return (i / 180.0d) * 3.141592653589793d;
    }

    public Route analyse(List<LatLng> list) {
        return analyse(list, 0);
    }

    public Route analyse(List<LatLng> list, int i) {
        if (list == null || list.isEmpty()) {
            return null;
        }
        long currentTimeMillis = System.currentTimeMillis();
        List<LatLng> convertPoly = convertPoly(list);
        Route buildRoute = buildRoute(convertPoly, PolyUtil.simplifyIndex(convertPoly, TOLERANCE), STEP_POLYLINE_TOLERANCE, i);
        Log.d("nav", "poly analyzer cost: " + (System.currentTimeMillis() - currentTimeMillis) + " ms");
        return buildRoute;
    }

    private List<LatLng> convertPoly(List<LatLng> list) {
        ArrayList arrayList = new ArrayList(list.size());
        for (LatLng latLng : list) {
            arrayList.add(BiCiCoorConverter.earth2Common(latLng));
        }
        return arrayList;
    }

    private List<Integer> compress(List<LatLng> list, double d, int i) {
        List<Integer> simplifyIndex = PolyUtil.simplifyIndex(list, d);
        return (i == 0 || simplifyIndex.size() <= i) ? simplifyIndex : compress(list, d * 1.5d, i);
    }

    private Route buildRoute(List<LatLng> list, List<Integer> list2, double d, int i) {
        double d2;
        ArrayList arrayList = new ArrayList(list2.size());
        for (int i2 = 0; i2 < list2.size(); i2++) {
            arrayList.add(list.get(list2.get(i2)));
        }
        double d4 = Utils.DOUBLE_EPSILON;
        LinkedList linkedList = new LinkedList();
        RouteStep routeStep = new RouteStep();
        int i3 = 1;
        if (arrayList.size() >= 3) {
            handleDirection(routeStep, (LatLng) arrayList.get(0), (LatLng) arrayList.get(1));
            int i4 = 0;
            int i5 = -1;
            while (true) {
                if (i4 >= arrayList.size() - i3) {
                    d2 = d;
                    break;
                }
                if (i5 < 0) {
                    i5 = list2.get(i4);
                }
                int i6 = i5;
                int i7 = i4 + 1;
                int intValue = list2.get(i7);
                double computeDistanceBetween = SphericalUtil.computeDistanceBetween((LatLng) arrayList.get(i4), (LatLng) arrayList.get(i7));
                d4 += computeDistanceBetween;
                if (i4 == arrayList.size() - 2) {
                    routeStep.setStartLoc(new RoutePoint(list.get(i6)));
                    routeStep.setEndLoc(new RoutePoint(list.get(intValue)));
                    d2 = d;
                    routeStep.setPolyline(buildPolyline(list.subList(i6, intValue + 1), d2, i));
                    routeStep.setDistance(formatDistance(computeDistanceBetween));
                    linkedList.add(routeStep);
                    break;
                }
                ArrayList arrayList2 = arrayList;
                double calAngle = NavDirectionHelper.calAngle((LatLng) arrayList.get(i4), (LatLng) arrayList.get(i7), (LatLng) arrayList.get(i4 + 2));
                if (Math.abs(calAngle) > 0.2617993877991494d) {
                    routeStep.setStartLoc(new RoutePoint(list.get(i6)));
                    routeStep.setEndLoc(new RoutePoint(list.get(intValue)));
                    routeStep.setPolyline(buildPolyline(list.subList(i6, intValue + 1), d, i));
                    routeStep.setDistance(formatDistance(computeDistanceBetween));
                    linkedList.add(routeStep);
                    routeStep = handleDirection(new RouteStep(), calAngle);
                } else {
                    intValue = i6;
                }
                i5 = intValue;
                arrayList = arrayList2;
                i4 = i7;
                i3 = 1;
            }
        } else {
            d2 = d;
            RouteStep routeStep2 = new RouteStep();
            routeStep2.setStartLoc(new RoutePoint(list.get(0)));
            routeStep2.setEndLoc(new RoutePoint(list.get(list.size() - 1)));
            routeStep2.setPolyline(buildPolyline(list, d2, i));
            double computeDistanceBetween2 = SphericalUtil.computeDistanceBetween((LatLng) arrayList.get(0), (LatLng) arrayList.get(list.size() - 1));
            d4 = Utils.DOUBLE_EPSILON + computeDistanceBetween2;
            routeStep.setDistance(formatDistance(computeDistanceBetween2));
            if (arrayList.size() >= 2) {
                handleDirection(routeStep, (LatLng) arrayList.get(0), (LatLng) arrayList.get(1));
            }
            linkedList.add(routeStep2);
        }
        RouteLeg routeLeg = new RouteLeg();
        RouteStep[] routeStepArr = new RouteStep[linkedList.size()];
        linkedList.toArray(routeStepArr);
        routeLeg.setSteps(routeStepArr);
        routeLeg.setStartLocation(new RoutePoint(list.get(0)));
        routeLeg.setEndLocation(new RoutePoint(list.get(list.size() - 1)));
        routeLeg.setDistance(formatDistance(d4));
        Route route = new Route();
        route.setLegs(new RouteLeg[]{routeLeg});
        route.setOverviewPolyline(buildPolyline(list, d2, i));
        return route;
    }

    private RouteStep handleDirection(RouteStep routeStep, double d) {
        if (d > 0.2617993877991494d) {
            if (d > 1.8325957145940461d) {
                routeStep.setHtmlInstruction(App.getContext().getString(R.string.direction_sharp_right));
                routeStep.setManeuver(RouteStep.MANEUVER_SHARP_RIGHT);
            } else if (d > 1.0471975511965976d) {
                routeStep.setHtmlInstruction(App.getContext().getString(R.string.direction_right));
                routeStep.setManeuver(RouteStep.MANEUVER_RIGHT);
            } else {
                routeStep.setHtmlInstruction(App.getContext().getString(R.string.direction_slight_right));
                routeStep.setManeuver(RouteStep.MANEUVER_SLIGHT_RIGHT);
            }
        } else if (d < -0.2617993877991494d) {
            double d2 = -d;
            if (d2 > 1.8325957145940461d) {
                routeStep.setHtmlInstruction(App.getContext().getString(R.string.direction_sharp_left));
                routeStep.setManeuver(RouteStep.MANEUVER_SHARP_LEFT);
            } else if (d2 > 1.0471975511965976d) {
                routeStep.setHtmlInstruction(App.getContext().getString(R.string.direction_left));
                routeStep.setManeuver(RouteStep.MANEUVER_LEFT);
            } else {
                routeStep.setHtmlInstruction(App.getContext().getString(R.string.direction_slight_left));
                routeStep.setManeuver(RouteStep.MANEUVER_SLIGHT_LEFT);
            }
        } else {
            routeStep.setHtmlInstruction(App.getContext().getString(R.string.direction_straight));
            routeStep.setManeuver(RouteStep.MANEUVER_STRAIGHT);
        }
        return routeStep;
    }

    private RouteStep handleDirection(RouteStep routeStep, LatLng latLng, LatLng latLng2) {
        String replaceAll;
        double calAngle = NavDirectionHelper.calAngle(new LatLng(latLng.latitude - Math.abs(latLng.latitude - latLng2.latitude), latLng.longitude), latLng, latLng2);
        double radian = toRadian(15);
        int i = R.string.direction_south;
        if (calAngle > radian) {
            if (calAngle <= toRadian(165)) {
                if (calAngle > toRadian(105)) {
                    i = R.string.direction_southeast;
                } else {
                    i = calAngle > toRadian(75) ? R.string.direction_east : R.string.direction_northeast;
                }
            }
        } else if (calAngle < (-toRadian(15))) {
            double d = -calAngle;
            if (d <= toRadian(165)) {
                if (d > toRadian(105)) {
                    i = R.string.direction_southwest;
                } else {
                    i = d > toRadian(75) ? R.string.direction_west : R.string.direction_northwest;
                }
            }
        } else {
            i = R.string.direction_north;
        }
        String string = App.getContext().getString(i);
        int computeDistanceBetween = (int) SphericalUtil.computeDistanceBetween(latLng, latLng2);
        if (computeDistanceBetween > 1000) {
            App context = App.getContext();
            replaceAll = context.getString(R.string.str_fm_unit_km_cn, " " + (computeDistanceBetween / 1000)).replaceAll(" ", "");
        } else {
            App context2 = App.getContext();
            replaceAll = context2.getString(R.string.str_fm_unit_m_cn, " " + computeDistanceBetween).replaceAll(" ", "");
        }
        routeStep.setHtmlInstruction(App.getContext().getString(R.string.nav_first_step, string, replaceAll));
        return routeStep;
    }

    private RoutePolyline buildPolyline(List<LatLng> list, double d, int i) {
        List<Integer> compress = compress(list, d, i);
        ArrayList arrayList = new ArrayList(compress.size());
        for (int i2 = 0; i2 < compress.size(); i2++) {
            arrayList.add(list.get(compress.get(i2)));
        }
        Log.i("PolyAnalyzer", "buildPolyline: original size=" + list.size() + ", simplified size=" + arrayList.size());
        return new RoutePolyline(PolyUtil.encode(arrayList));
    }

    private RouteStep.IntValuePair formatDistance(double d) {
        RouteStep.IntValuePair intValuePair = new RouteStep.IntValuePair();
        intValuePair.setValue((int) d);
        intValuePair.setText(SphericalUtil.formatDistance(d));
        return intValuePair;
    }
}